<?php
class news extends database{
// add ------
	public function Add($useradmin, $type, $status, $sort, $image){				
		if(!$this->isChar($useradmin)){
			return 3;
		}else if(!$this->isInt($type)){
			return 4;
		}else if(!$this->isInt($status)){
			return 5;
		}else if(!$this->isInt($sort)){
			return 6;
		}else if(!$this->isText($image)){
			return 7;
		}else if(!$this->isLen($image,1,200)){
			return 8;
		}else{			
			$sql="insert into news(useradmin, type, status, sort, image, create_date) values('".$useradmin."', ".$type.", ".$status.", ".$sort.", '".$image."', now())";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// edit ------
	public function Edit($id_news, $useradmin, $type, $status, $sort, $image){				
		if(!$this->isInt($id_news)){
			return 2;
		}else if(!$this->isChar($useradmin)){
			return 3;
		}else if(!$this->isInt($type)){
			return 4;
		}else if(!$this->isInt($status)){
			return 5;
		}else if(!$this->isInt($sort)){
			return 6;
		}else if(!$this->isText($image)){
			return 7;
		}else if(!$this->isLen($image,1,200)){
			return 8;
		}else{			
			$sql="update news set useradmin='".$useradmin."', type=".$type.", status=".$status.", sort=".$sort.", image='".$image."' where id_news=".$id_news."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// Del ------
	public function Del($id){				
		if(!$this->isInt($id)){
			return 2;
		}else{			
			$sql="delete from news where id_news=".$id."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// get by id
	public function GetById($id_news){	
		if(!$this->isInt($id_news)){
			return 2;
		}else{	
			$sql="select id_news, useradmin, type, status, sort, image, create_date from news where id_news=".$id_news."";
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get all
	public function GetByAll($Top=0, $Position=0, $type=0, $status=10){	
		if(!$this->isInt($Top) || !$this->isInt($Position) ){
			return 2;	
		}else if(!$this->isInt($type)){
			return 3;
		}else if(!$this->isInt($status)){
			return 4;
		}else{	
			if($type==0){
				$type	= " type<>0";	
			}else{
				$type	= " type=".$type;	
			}
			if($status==10){
				$status	= " and status<>2";	
			}else{
				$status	= " and status=".$status;	
			}
			if($Top>0){
				$Top=" limit ".$Position.", ".$Top." ";
			}else{
				$Top="";
			}
			$sql="select id_news, useradmin, type, status, sort, image, create_date from news where ".$type.$status." order by id_news desc ".$Top;
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get MaxIdCount
	public function CountByAll($type, $status=10){		
		if(!$this->isInt($type)){
			return 3;
		}else if(!$this->isInt($status)){
			return 4;
		}
		if($type==0){
			$type	= " type<>0";	
		}else{
			$type	= " type=".$type;	
		}
		if($status==10){
			$status	= " and status<>2";	
		}else{
			$status	= " and status=".$status;	
		}
		$sql="select count(*) as count from news where ".$type.$status."";
		if($result=$this->query($sql)){
			$a=$this->fetch($result);
			return $a['count'];
		}
		return 0;
	}
// get MaxId
	public function GetMaxId(){			
		$sql="select max(id_news) as id_news from news";
		if($result=$this->query($sql)){
			$a=$this->fetch($result);
			return $a['id_news'];
		}
		return 0;
	}
// get MaxId
	public function GetMaxSort(){			
		$sql="select max(sort) as id_news from news";
		if($result=$this->query($sql)){
			$a=$this->fetch($result);
			return $a['id_news'];
		}
		return 0;
	}
}
?>